package utils.encode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomTicketNo {
	private Random random;
	private String table;
	private SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static RandomTicketNo instance = new RandomTicketNo();

	public static RandomTicketNo getInstance() {
		return instance;
	}

	public static String random() {
		return instance.random(instance.sf.format(new Date()));
	}

	public static String random(int bit) {
		return instance.random(instance.sf.format(new Date()), bit);
	}

	private RandomTicketNo() {
		random = new Random();
		table = "0123456789";
	}

	public String randomId(int id, int length) {
		String num = String.format("%0" + length + "d", id); // String.format("%07d",
																// id);
		return random(num);
	}

	public String random(String source) {
		String ret = null, num = source;
		int key = random.nextInt(10), seed = random.nextInt(100);
		Caesar caesar = new Caesar(table, seed);
		num = caesar.encode(key, num);
		ret = num + String.format("%01d", key) + String.format("%02d", seed);

		return ret;
	}

	public String random(String source, int bit) {
		String ret = null, num = source.substring(4, bit + 1);
		int key = random.nextInt(10), seed = random.nextInt(100);
		Caesar caesar = new Caesar(table, seed);
		num = caesar.encode(key, num);
		ret = num + String.format("%01d", key) + String.format("%02d", seed);

		return ret;
	}

	public static void main(String[] args) {
		RandomTicketNo r = new RandomTicketNo();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		for (int i = 0; i < 30; i += 1) {
		}
	}
}